package hhm.zimu;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;

/**
 * 修改文件
 */
public class FileModify {

	/**
	 * 读取文件内容
	 * 
	 * @param filePath
	 * @return
	 */
	public String read(String filePath) {
		BufferedReader br = null;
		String line = null;
		StringBuffer buf = new StringBuffer();

		try {
			// 根据文件路径创建缓冲输入流
			br = new BufferedReader(new FileReader(filePath));

			// 循环读取文件的每一行, 对需要修改的行进行修改, 放入缓冲对象中
			while ((line = br.readLine()) != null) {
				// 此处根据实际需要修改某些行的内容
				if (line.startsWith("a")) {
					buf.append(line).append(" start with a");
				} else if (line.startsWith("b")) {
					buf.append(line).append(" start with b");
				}
				// 如果不用修改, 则按原来的内容回写
				else {
					buf.append(line);
				}
				buf.append(System.getProperty("line.separator"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 关闭流
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					br = null;
				}
			}
		}

		return buf.toString();
	}

	/**
	 * 将内容回写到文件中
	 * 
	 * @param filePath
	 * @param content
	 */
	public void write(String filePath, String content) {
		BufferedWriter bw = null;

		try {
			// 根据文件路径创建缓冲输出流
			bw = new BufferedWriter(new FileWriter(filePath));
			// 将内容写入文件中
			bw.write(content);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			// 关闭流
			if (bw != null) {
				try {
					bw.close();
				} catch (IOException e) {
					bw = null;
				}
			}
		}
	}

	public void fileAppender(String fileName, String content)
			throws IOException {

		BufferedReader reader = new BufferedReader(new FileReader(fileName));
		String line = null;
		// 一行一行的读
		StringBuilder sb = new StringBuilder();

		sb.append(content);

		reader.close();

		// 写回去
		RandomAccessFile mm = new RandomAccessFile(fileName, "rws");
		mm.writeBytes(sb.toString());
		mm.close();
	}

	public void fileAppender1(String fileName, String content) throws Exception

	{
		BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
				new FileOutputStream(fileName)));
		writer.write(content);
		writer.flush();
		writer.close();
	}

	/**
	 * 主方法
	 */
	public static void main(String[] args) {
		String filePath = FileModify.class.getResource("").getPath()
				+ "test.properties"; // 文件路径
		FileModify obj = new FileModify();
		obj.write(filePath, obj.read(filePath)); // 读取修改文件
		try {
			obj.fileAppender(filePath, "set a=b \n");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * 用FileWrite向文件写入内容
	 * 
	 * 
	 * 
	 * @param _destFile
	 * 
	 * @throws IOException
	 */

	public static void writeByFileWrite(String _sDestFile, String _sContent)

	throws IOException {

		FileWriter fw = null;

		try {

			fw = new FileWriter(_sDestFile);

			fw.write(_sContent);

		} catch (Exception ex) {

			ex.printStackTrace();

		} finally {

			if (fw != null) {

				fw.close();

				fw = null;

			}

		}

	}

	/**
	 * 
	 * 用FileOutputStream向文件写入内容
	 * 
	 * 
	 * 
	 * @param _destFile
	 * 
	 * @throws IOException
	 */

	public static void writeByFileOutputStream(String _sDestFile,

	String _sContent) throws IOException {

		FileOutputStream fos = null;

		try {

			fos = new FileOutputStream(_sDestFile);

			fos.write(_sContent.getBytes());

		} catch (Exception ex) {

			ex.printStackTrace();

		} finally {

			if (fos != null) {

				fos.close();

				fos = null;

			}

		}

	}

	/**
	 * 
	 * 用OutputStreamWrite向文件写入内容
	 * 
	 * 
	 * 
	 * @param _destFile
	 * 
	 * @throws IOException
	 */

	public static void writeByOutputStreamWrite(String _sDestFile,

	String _sContent) throws IOException {

		OutputStreamWriter os = null;

		FileOutputStream fos = null;

		try {

			fos = new FileOutputStream(_sDestFile);

			os = new OutputStreamWriter(fos, "UTF-8");

			os.write(_sContent);

		} catch (Exception ex) {

			ex.printStackTrace();

		} finally {

			if (os != null) {

				os.close();

				os = null;

			}

			if (fos != null) {

				fos.close();

				fos = null;

			}

		}

	}

	public static void appendMethodA(String fileName, String content) {
		try {
			// 打开一个随机访问文件流，按读写方式
			RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
			// // 文件长度，字节数
			// long fileLength = randomFile.length();
			// // 将写文件指针移到文件尾。
//			randomFile.seek(120);

			randomFile.write(content.getBytes("utf-8"));
			randomFile.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
